/**
 * @FileName index
 * @Description
 * @Author renminghe
 * @Date 2019-04-03 09:56
 * @Version 1.0
 * @Copyright(c) gome inc Gome Co.,LTD
 **/

var clock = null;
function Toast(c, b) {
    b = isNaN(b) ? 2000 : b;
    var a = document.createElement("div");
    a.innerHTML = c;
    a.style.cssText = "min-width: 15rem; max-width:22rem; opacity: 0.7;    color: rgb(255, 255, 255); padding:1.2rem 1.5rem 1.2rem 1.5rem;    text-align: center;border-radius: 0.25rem;position: fixed;    z-index: 999999;    background: rgb(0, 0, 0);font-size: 1.5rem;";
    document.body.appendChild(a);
    a.style.left = (document.documentElement.clientWidth - a.clientWidth) * 0.5 + "px";
    a.style.top = document.documentElement.clientHeight * 0.35 + "px";
    setTimeout(function () {
        var e = 0.5;
        a.style.webkitTransition = "-webkit-transform " + e + "s ease-in, opacity " + e + "s ease-in";
        a.style.opacity = "0";
        setTimeout(function () {
            document.body.removeChild(a)
        }, e * 1000)
    }, b)
}
function resizeUI(e, i) {
    var f = $("#background").get(0);
    if (f) {
        var d = f.naturalWidth;
        var g = f.naturalHeight;
        var c = g / d * e;
        $("#container").css("width", e + "px").css("height", i + "px")
    }
    var b = i / g * 100;
    var a = document.styleSheets[0].rules[0];
    a.style.fontSize = b + "px"
}
function onResizeWindow() {
    var a = document.documentElement.clientWidth;
    var b = document.documentElement.clientHeight;
    resizeUI(a, b);
    Halo.Config.onResizeWindow(a, b);

    setLodingWidget();
}
function onMouseDown(b) {
    var a = b.clientX;
    var c = document.documentElement.clientHeight - b.clientY;
    Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_DOWN, a, c)
}
function onMouseUp(b) {
    var a = b.clientX;
    var c = document.documentElement.clientHeight - b.clientY;
    Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_UP, a, c)
}
function onMouseMove(b) {
    var a = b.clientX;
    var c = document.documentElement.clientHeight - b.clientY;
    Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_Move, a, c)
}
var touch_identifier = null;
function onTouchStart(b) {
    if (b.changedTouches.length === 1) {
        var c = b.changedTouches[0];
        var a = c.pageX;
        var d = document.documentElement.clientHeight - c.pageY;
        touch_identifier = c.identifier;
        Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_DOWN, a, d)
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(b)
    }
}
function onTouchEnd(b) {
    if (b.changedTouches.length === 1) {
        var c = b.changedTouches[0];
        if (touch_identifier === c.identifier) {
            var a = c.pageX;
            var d = document.documentElement.clientHeight - c.pageY;
            Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_UP, a, d)
        }
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(b)
    }
}
function onTouchMove(b) {
    if (b.changedTouches.length === 1) {
        var c = b.changedTouches[0];
        if (touch_identifier === c.identifier) {
            var a = c.pageX;
            var d = document.documentElement.clientHeight - c.pageY;
            Halo.Config.scene() && Halo.Config.scene().handleEvent(b, Halo.EVT_UI_MOVE, a, d)
        }
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(b)
    }
}
function onKeyDown(a) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(a)
}
function onContextMenu(a) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(a)
}
function onMouseWheel(a) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(a)
}
function mainloop() {
    window.onerror = reportErr;

    function reportErr(params) {
        alert(params);
    }

    try {

        if (clock === null) {
            clock = new Halo.Clock()
        }

        Halo.Config.update(clock.getDelta());

        requestAnimationFrame(mainloop)

    } catch (error) {
        alert(error);
    }
}
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1 || ua.indexOf("adr") > -1;
var isios = ua.indexOf("ipad") > -1 || ua.indexOf("iphone") > -1;
var isWeixinBrowser = /micromessenger/.test(ua);

function getQueryString(a) {
    var b = new RegExp("(^|&)" + a + "=([^&]*)(&|$)", "i");
    var c = window.location.search.substr(1).match(b);
    if (c != null) {
        return decodeURI(c[2])
    }
    return null
}
function check_webgl() {
    var b = document.createElement("canvas");
    var c = null;
    try {
        c = b.getContext("webgl")
    } catch (a) {
        c = null
    }
    if (c === null) {
        try {
            c = b.getContext("experimental-webgl");
            experimental = true
        } catch (a) {
            c = null
        }
    }
    if (c === null) {
        $(".page").css("display", "none");
        $("#background").attr("src", "res/not_support.jpg");
        return false
    }
    return true
}
function compatible() {
    if (ArrayBuffer.isView === undefined) {
        ArrayBuffer.isView = function () {
            return false
        }
    }
}
function init() {

    var evt = "onorientationchange" in window ? "orientationchange" : "resize";
    window.addEventListener(evt, reloadView, false);

    var i = document.documentElement.clientWidth;
    var n = document.documentElement.clientHeight;
    resizeUI(i, n);
    if (!check_webgl()) {
        return
    }
    compatible();
    $(".page").css("display", "block");
    var d = new ProgressBar.Line("#circle", {
        color: "#1da7ff",
        trailColor: "#13171e66",
        duration: 3000,
        easing: "easeInOut",
        svgStyle: {
            width: "70%",
            height: "8px",
            margin: "0 auto",
            borderTopLeftRadius: "6px",
            borderTopRightRadius: "6px",
            borderBottomRightRadius: "5px",
            borderBottomLeftRadius: "5px",
            overFlow: "hidden"
        }
    });

    setLodingWidget();

    d.set(0);

    var e =  getQueryString("modelUrl");
    var g = new XMLHttpRequest();
    g.open("GET", e, true);
    g.send();
    g.onreadystatechange = function () {
        if (g.readyState === XMLHttpRequest.DONE) {
            if (g.status === 200 && g.readyState === 4) {
                try {
                    a(g.response)
                } catch (h) {
                    alert(h)
                }
            } else {
                if (g.status === 0) {
                    return
                }
                Toast("error")
            }
        }
    }
    ;
    g.responseType = "arraybuffer";
    if (g.overrideMimeType) {
        g.overrideMimeType("text/plain")
    }
    g.onprogress = function (h) {
        if (h.lengthComputable) {
            var t = h.loaded / h.total;
            d.set(t * 0.98);
            $("#loading").html("loading " + Math.floor(t * 98) + "%")
        }
    }
    ;
    function a(w) {
        var y = new JSZip(w, {
            "uint8array": true
        });
        var v = Object.keys(y.files);
        var u = y.files[v[0]].asText();
        var x = JSON.parse(u);

        var t = "";
        if (x.cfg.version == '0.1f') {
            t = "http://ddd.gome.com.cn/js/halo-new.min.js";
        } else {
            t = "http://ddd.gome.com.cn/js/halo-" + x.cfg.version + ".min.js";
        }

        var h = document.createElement("script");
        h.type = "text/javascript";
        h.onload = function () {
            d.set(0.99);
            $("#loading").html("loading 99%");
            window.addEventListener("keydown", onKeyDown, false);
            document.addEventListener("contextmenu", onContextMenu, false);
            document.addEventListener("wheel", onMouseWheel, false);
            if (!isAndroid && !isios) {
                document.addEventListener("mousedown", onMouseDown, false);
                document.addEventListener("mouseup", onMouseUp, false);
                document.addEventListener("mousemove", onMouseMove, false)
            }
            document.addEventListener("touchstart", onTouchStart, false);
            document.addEventListener("touchend", onTouchEnd, false);
            document.addEventListener("touchmove", onTouchMove, false);
            window.addEventListener("resize", onResizeWindow, false);

            Halo.init(null, "", null, function () {
                d.set(1);
                $("#loading").html("loading 100%");
                Halo.parseProject(x, function (z) {
                    if (z === null) {
                        try {
                            Halo.Config.start(document.body)
                        } catch (A) {
                            alert("Halo.Config.start Failed:" + A)
                        }
                        try {
                            Halo.Config.play()
                        } catch (A) {
                            alert("Halo.Config.play Failed:" + A)
                        }
                        $(".page").remove();
                        $("#container").remove();
                        $(".airConditioning").remove();
                        onResizeWindow();
                        mainloop()
                    }
                })
            })
        }
        ;
        h.src = t;
        document.head.appendChild(h)
    }
}
if (isWeixinBrowser) {
    document.addEventListener("WeixinJSBridgeReady", function () {
        init()
    }, false)
} else {
    window.onload = function () {
        try {
            init()
        } catch (a) {
            Toast("INIT FAILED!")
        }
    }
};

function setLodingWidget() {

    var s = window.innerHeight;
    var l = window.innerWidth;
    var r = l / 2 - $("#loading").width() / 2;
    var k = $(".progress").width();
    var m = l / 2 - k / 2;
    var t = s / 2 - 25

    $("#container").css("height", s + "px");
    $("#loading").css({
        "left": r,
        "bottom": t
    });
    $(".progress").css({
        "left": m,
        "bottom": s / 2
    });


}

function reloadView () {
    if ($("#container").length) window.location.reload();
}

